//
//  AttentionStudioController.m
//  Arts1
//  画室页面
//  Created by apple on 15/9/21.
//  Copyright © 2015年 Arts1. All rights reserved.
//

#import "AttentionStudioController.h"
#import "StudioCollectionViewCell.h"
#import "AttentionStudioModel.h"
#import "AllHeader.h"

@interface AttentionStudioController ()

//获取数据源
@property (nonatomic ,strong) NSMutableArray *dataSource;

@end

@implementation AttentionStudioController

static NSString * const reuseIdentifier = @"Cell";

- (NSMutableArray *)dataSource
{
    if (_dataSource == nil)
    {
        self.dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.collectionView.backgroundColor = kBackgroundColor;
    
    [self.collectionView registerClass:[StudioCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    //设置没有铺满也可以滚动
    self.collectionView.alwaysBounceVertical = YES;
    
   
}
- (void)viewDidAppear:(BOOL)animated
{
    //请求数据
    [self requestData];
}

//请求数据
- (void)requestData
{
    AFHTTPRequestOperationManager *manage = [AFHTTPRequestOperationManager manager];
    
    manage.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    manage.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSDictionary *dic = [[NSUserDefaults standardUserDefaults] objectForKey:@"dataForKeyAndUid"];
    
    NSString *uid = dic[@"data"][@"uid"];
    
    NSString *key = dic[@"data"][@"key"];
    
    NSDictionary *pardic = [NSDictionary dictionary];
    
    if (uid && key)
    {
        pardic = @{@"uid":uid,
                   @"key":key,
                   @"type":@"画室"};
    }
    
    [manage POST:@"http://arts1.dlab.com.cn/app/myspace" parameters:pardic
         success:^(AFHTTPRequestOperation *operation, id responseObject) {
             NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:(NSJSONReadingMutableContainers) error:nil];
             //返回的状态
             int status = [dic[@"status"] intValue];
             if (status == 1)
             {
                 NSArray *list = dic[@"list"];
                 
                 for (NSDictionary *dict in list)
                 {
                     AttentionStudioModel *model = [[AttentionStudioModel alloc] init];
                     [model setValuesForKeysWithDictionary:dict];
                     [self.dataSource addObject:model];
                 }
             }
             
             [self.collectionView reloadData];
             
             // 请求成功的时候调用这个block
             NSLog(@"画室数据请求--%@", dic);
         } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             // 请求失败的时候调用调用这个block
             NSLog(@"请求失败");
         }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    StudioCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    AttentionStudioModel *model = _dataSource[indexPath.row];
    cell.model = model;
    // Configure the cell
    
    return cell;
}

#pragma mark <UICollectionViewDelegate>
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 10, 10, 10);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(SCREEN_WIDTH - 20, 70);
    
}

@end
